package com.ericom.webapp.action;

import java.util.List;

import com.ericom.model.ShippingMethod;
import com.ericom.service.GenericManager;

public class ShippingMethodAction extends BaseAction {
	private GenericManager<ShippingMethod, Long> shippingMethodManager;
	private List shippingMethods;
	private ShippingMethod shippingMethod;
	private Long id;

	public ShippingMethod getShippingMethod() {
		return shippingMethod;
	}

	public void setShippingMethod(ShippingMethod shippingMethod) {
		this.shippingMethod = shippingMethod;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setShippingMethodManager(GenericManager<ShippingMethod, Long> shippingMethodManager) {
		this.shippingMethodManager = shippingMethodManager;
	}

	public String list() {
		shippingMethods = shippingMethodManager.getAll();
		return SUCCESS;
	}

	public List getShippingMethods() {
		return shippingMethods;
	}

	public String delete() {
		shippingMethodManager.remove(shippingMethod.getId());
		this.saveMessage(getText("shippingMethod.deleted"));
		return this.SUCCESS;
	}

	public String edit() {
		if (id != null) {
			shippingMethod = shippingMethodManager.get(id);
		} else {
			shippingMethod = new ShippingMethod();
		}
		return this.SUCCESS;
	}

	public String save() throws Exception {
		if (this.cancel != null) {
			return this.CANCEL;
		}
		if (this.delete != null) {
			return this.delete();
		}

		boolean isNew = (shippingMethod.getId() == null);
		shippingMethod = shippingMethodManager.save(shippingMethod);

		String key = (isNew) ? "shippingMethod.added" : "shippingMethod.updated";
		this.saveMessage(getText(key));

		if (!isNew) {
			return this.INPUT;
		} else {
			return this.SUCCESS;
		}
	}
}
